package com.laundry.controller;

import com.laundry.dto.ResultDTO;
import com.laundry.entity.ServiceType;
import com.laundry.service.ServiceTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/service")
public class ServiceTypeController {
    
    @Autowired
    private ServiceTypeService serviceTypeService;
    
    @GetMapping("/list")
    public ResultDTO getServiceTypes() {
        try {
            return ResultDTO.success(serviceTypeService.findAll());
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取服务类型列表失败");
        }
    }
    
    @GetMapping("/detail")
    public ResultDTO getServiceTypeById(@RequestParam Long id) {
        try {
            return ResultDTO.success(serviceTypeService.findById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("获取服务类型详情失败");
        }
    }
    
    @PostMapping("/create")
    public ResultDTO createServiceType(@RequestBody ServiceType serviceType) {
        try {
            return ResultDTO.success(serviceTypeService.save(serviceType));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("创建服务类型失败");
        }
    }
    
    @PutMapping("/update")
    public ResultDTO updateServiceType(@RequestBody ServiceType serviceType) {
        try {
            return ResultDTO.success(serviceTypeService.update(serviceType));
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("更新服务类型失败");
        }
    }
    
    @DeleteMapping("/delete")
    public ResultDTO deleteServiceType(@RequestParam Long id) {
        try {
            serviceTypeService.deleteById(id);
            return ResultDTO.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDTO.error("删除服务类型失败");
        }
    }
} 